<?php

namespace app\admin\command;

use InvalidArgumentException;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Testcmd extends Command
{
    protected function configure()
    {
        $this->setName('testcmd')->setDescription('Test command');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->info("Test command executed successfully!");

        /**
         * 将数字金额转换为法语几内亚法郎大写形式
         * 
         * @param float $number 要转换的金额数字
         * @return string 转换后的法语大写金额
         * @throws InvalidArgumentException 当输入不是有效数字时抛出
         */
        function convertToFrenchCurrency($number)
        {
            // 验证输入
            if (!is_numeric($number)) {
                throw new InvalidArgumentException("输入必须是有效的数字");
            }

            // 处理为整数
            $integerPart = (int)round($number);

            // 法语数字和货币配置
            $config = [
                'ones' => [
                    0 => 'zéro',
                    1 => 'un',
                    2 => 'deux',
                    3 => 'trois',
                    4 => 'quatre',
                    5 => 'cinq',
                    6 => 'six',
                    7 => 'sept',
                    8 => 'huit',
                    9 => 'neuf',
                    10 => 'dix',
                    11 => 'onze',
                    12 => 'douze',
                    13 => 'treize',
                    14 => 'quatorze',
                    15 => 'quinze',
                    16 => 'seize',
                    17 => 'dix-sept',
                    18 => 'dix-huit',
                    19 => 'dix-neuf'
                ],
                'tens' => [
                    0 => '',
                    1 => 'dix',
                    2 => 'vingt',
                    3 => 'trente',
                    4 => 'quarante',
                    5 => 'cinquante',
                    6 => 'soixante',
                    7 => 'soixante-dix',
                    8 => 'quatre-vingt',
                    9 => 'quatre-vingt-dix'
                ],
                'hundreds' => [
                    0 => '',
                    1 => 'cent',
                    2 => 'deux cents',
                    3 => 'trois cents',
                    4 => 'quatre cents',
                    5 => 'cinq cents',
                    6 => 'six cents',
                    7 => 'sept cents',
                    8 => 'huit cents',
                    9 => 'neuf cents'
                ],
                'large_units' => [
                    1 => '',
                    1000 => 'mille',
                    1000000 => 'million',
                    1000000000 => 'milliard'
                ],
                'currency' => [
                    'singular' => 'franc guinéen',
                    'plural' => 'francs guinéens'
                ],
                'conjunctions' => [
                    'and' => ' et ',
                    'hyphen' => '-'
                ]
            ];

            // 转换整数部分
            $integerStr = convertInteger($integerPart, $config);

            // 处理货币单位
            $currencyStr = ($integerPart == 1 ? $config['currency']['singular'] : $config['currency']['plural']);

            // 组合结果
            $result = $integerStr . ' ' . $currencyStr;

            // 首字母大写并返回
            return ucfirst(trim($result));
        }

        /**
         * 转换整数部分（内部辅助函数）
         */
        function convertInteger($number, $config)
        {
            if ($number == 0) {
                return $config['ones'][0];
            }

            $result = [];
            $units = array_reverse(array_keys($config['large_units']));

            foreach ($units as $unit) {
                if ($number >= $unit) {
                    $count = (int)($number / $unit);
                    $number = $number % $unit;

                    if ($count > 0) {
                        $unitStr = '';
                        if ($unit > 1) {
                            $unitStr = ' ' . $config['large_units'][$unit];
                            // 处理复数形式
                            if ($count > 1 && $unit >= 1000000) {
                                $unitStr .= 's';
                            }
                        }

                        // 转换当前单位的数量
                        $countStr = convertThreeDigits($count, $config);

                        // 特殊处理：1000的情况，法语中"un mille"应简化为"mille"
                        if ($unit == 1000 && $count == 1) {
                            $result[] = $unitStr;
                        } else {
                            $result[] = $countStr . $unitStr;
                        }
                    }
                }
            }

            return trim(implode(' ', $result));
        }

        /**
         * 转换三位数（内部辅助函数）
         */
        function convertThreeDigits($number, $config)
        {
            if ($number == 0) {
                return '';
            }

            $parts = [];
            $hundreds = (int)($number / 100);
            $remainder = $number % 100;

            if ($hundreds > 0) {
                // 法语中100是"cent"，200是"deux cents"（复数）
                $parts[] = ($hundreds == 1) ? $config['hundreds'][$hundreds] : $config['hundreds'][$hundreds];
            }

            if ($remainder > 0) {
                $parts[] = convertTwoDigits($remainder, $config);
            }

            // 处理"cent"的复数形式（当没有余数且百位数大于1时）
            if ($hundreds > 1 && $remainder == 0) {
                $lastPart = end($parts);
                if ($lastPart === 'cent') {
                    array_pop($parts);
                    $parts[] = 'cents';
                }
            }

            return trim(implode(' ', $parts));
        }

        /**
         * 转换两位数（内部辅助函数）
         */
        function convertTwoDigits($number, $config)
        {
            if ($number == 0) {
                return '';
            }

            if ($number < 20) {
                return $config['ones'][$number];
            }

            $tens = (int)($number / 10);
            $ones = $number % 10;

            $result = $config['tens'][$tens];

            if ($ones > 0) {
                // 特殊情况处理
                if ($tens == 7) { // 70-79
                    $result .= $config['conjunctions']['hyphen'] . $config['ones'][$ones + 10];
                } elseif ($tens == 9) { // 90-99
                    $result .= $config['conjunctions']['hyphen'] . $config['ones'][$ones + 10];
                } elseif ($tens == 8) { // 80-89
                    $result .= $config['conjunctions']['hyphen'] . $config['ones'][$ones];
                } elseif ($tens == 2 && $ones == 1) { // 21
                    $result .= $config['conjunctions']['and'] . $config['ones'][$ones];
                } else {
                    $result .= $config['conjunctions']['hyphen'] . $config['ones'][$ones];
                }
            } elseif ($tens == 8) { // 80
                $result = 'quatre-vingts';
            }

            return $result;
        }

        // 测试1431400这个数字 - 应输出"Un million quatre cent trente et un mille quatre cents francs guinéens"
        // 对应的中文是"一百四十三万一千四百几内亚法郎"
        // echo "1431400 => " . convertToFrenchCurrency(1431400) . "\n";
        echo "16750000 => " . convertToFrenchCurrency(16750000) . "\n";
    }
}
